package com.bdd.crowd.mvc.handler;

import com.bdd.crowd.entity.Admin;
import com.bdd.crowd.entity.ParamData;
import com.bdd.crowd.entity.Student;
import com.bdd.crowd.service.api.AdminService;
import com.bdd.crowd.utils.CrowdUtil;
import com.bdd.crowd.utils.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author banddong
 * @create 2020-08-16 16:06
 * 测试SSM整合环境
 */
@Controller
public class TestHandler {

    /**
     * 注入service层
     */
    @Autowired
    private AdminService adminService;

    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(TestHandler.class);

    /**
     * 一览
     * @param modelMap
     * @param request
     * @return
     */
    @RequestMapping("/test/ssm")
    public String testHandlerSelectAdmin(ModelMap modelMap, HttpServletRequest request){

        //测试判断请求类型
        boolean judgeRequestType = CrowdUtil.judgeRequestType(request);

        logger.info("judgeRequestType = "+ judgeRequestType );

        //得到一览信息
        List<Admin>adminList = adminService.getAll();
        //放入modelMap模型中/或者model
        modelMap.addAttribute("adminList",adminList);

        return "target";
    }


    /**
     * Ajax的请求方式
     *方式一
     */
    @ResponseBody
    @RequestMapping("/send/array/one")
    public String testAjax1(@RequestParam("array[]") List<Integer> array){
        //缺陷：handler方法中接收“数组”数据时需要在请求参数后面多加一个“[]”，不建议使用
        for (Integer number : array){
            System.out.println(number);
        }
        return "success";
    }


    /**
     * Ajax的请求方式
     *方式二
     */
    @ResponseBody
    @RequestMapping("/send/array/two.html")
    public String testAjax2(ParamData param){//通过创建一个实体来接收参数

        List<Integer> array = param.getArray();

        for (Integer number : array){
            System.out.println("number = "+number);
        }
        return "success";
    }

    /**
     * Ajax的请求方式
     *方式三
     */
    @ResponseBody
    @RequestMapping("/send/array/three")
    public String testAjax3(@RequestBody List<Integer> array){

        for(Integer number : array){
            logger.info("number = "+number);
        }
        return "success";
    }


    /**
     * 复杂的Ajax请求
     * @param student
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/send/compose/object.json")
    public ResultEntity<Student> testComposeObject(@RequestBody Student student,HttpServletRequest request){

        boolean judgeRequestType = CrowdUtil.judgeRequestType(request);

        logger.info("judgeRequestType = "+ judgeRequestType);

        logger.info(student.toString());

        // 将“查询”到的Student对象封装到ResultEntity中返回
        ResultEntity<Student> resultEntity = ResultEntity.successWithData(student);

        //空指针异常
        String a = null;
        System.out.println(a.length());

        return resultEntity;
       // return ResultEntity.successWithData(student);
    }
}
